<template>
  <div>
    <Header></Header>
    <div class="p-40 button-nav">
      <div class="" style="width: 800px;margin: auto">
        <div class="pt-10 pb-10 pl-20 pr-20 bg-grey flex font-14">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>考生注册</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="bg-shadow mt-20 p-40 bg-white">
          <el-form :model="form" label-width="120px"
                   ref="formRef" :rules="rules"
                   style="width: 50%;margin: auto;">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" placeholder="请输入姓名"/>
            </el-form-item>
            <el-form-item label="身份证号" prop="id_card">
              <el-input v-model="form.id_card" placeholder="请输入身份证号"/>
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" type="password"/>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.sex" class="ml-4">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="所属单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入所属单位"/>
            </el-form-item>
            <el-form-item label="单位性质" prop="unit_type">
              <el-select v-model="form.unit_type" class="m-2" placeholder="请选择单位性质">
                <el-option
                    v-for="(item, index) in unit_type"
                    :key="index"
                    :label="item"
                    :value="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="职务" prop="job">
              <el-input v-model="form.job" placeholder="请填写职务全称"/>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入联系电话"/>
            </el-form-item>
            <el-form-item label="考生类别" prop="type">
              <el-select v-model="form.type" class="m-2" placeholder="请选择考生类别">
                <el-option
                    v-for="(item, index) in type"
                    :key="index"
                    :label="item"
                    :value="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">注册</el-button>
              <el-button @click="goBack">返回</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";

import { register } from '@/api/api.js'

const rules = {
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  id_card: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  unit_type: [
    { required: true, message: '请选择单位性质', trigger: 'blur' }
  ],
  job: [
    { required: true, message: '请填写职务全称', trigger: 'blur' }
  ],
  mobile: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择考生类别', trigger: 'blur' }
  ],
  unit: [
    { required: true, message: '请输入所属单位', trigger: 'blur' }
  ]
}

export default {
  name: "Register",
  components: {
    Header, Footer
  },
  data() {
    return {
      form: {
        username: '',
        password: '',
        id_card: '',
        unit: '',
        unit_type: '',
        job: '',
        mobile: '',
        type: '',
        sex: 1,
      },
      rules: rules,
      unit_type: ['党政机关', '事业单位', '国有企业', '群团组织', '垂管部门'],
      type: ['厅局级', '县处级', '科级', '股级', '科员', '国企负责人', '国企中层', '其他国家工作人员'],
    }
  },
  mounted() {
    const _this = this
    const user = JSON.parse(localStorage.getItem('xx-token'))
    if (user) {
      _this.$router.push('/')
    }
  },
  methods: {
    onSubmit() {
      const _this = this
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error('请填写或选择所有必填项')
          return false
        }
        // operation code
        const data = await register(this.form)
        // console.log('返回',data)
        if (data) {
          this.$message.success('注册成功，请登录')
          setTimeout(()=>{
            _this.$router.push('/login')
          }, 1000)
        }
      })
    },
    goBack() {
      this.$router.back()
    },
  }
}
</script>

<style scoped>

</style>
